<?php 
	if(isset($message)){
		echo $message;
	} else {
?>

<h1>Education</h1>
<?php echo form_open('user/education');?>
<table>
	<?php if(isset($education)){?>
	<tr>
		<td colspan="2">
			<table>
				<tr>
					<td>School:</td>
					<td><input type="text" name="school_name[]" value="<?php echo $education->school_name?>">
					<input type="hidden" name="id[]" value="<?php echo $education->id?>">
					</td>
				</tr>
				<tr>
					<td>Course:</td>
					<td><input type="text" name="course_name[]" value="<?php echo $education->course_name?>">
					</td>
				</tr>
				<tr>
					<td>Date Started:</td>
					<td><input type="text" name="date_start[]" id="date_start" value="<?php echo $education->date_start?>"></td>
				</tr>
				<tr>
					<td>Date Ended:</td>
					<td><input type="text" name="date_end[]" id="date_end" value="<?php echo $education->date_end?>"></td>
				</tr>
				<tr>
					<td>Type:</td>
					<td>
						<select name="type[]">
						<option value="COLLEGE" <? if(isset($education->type)){ echo ($education->type == 'COLLEGE')?'selected':'';}?>>College</option>
						<option value="VOCATIONAL" <? if(isset($education->type)){ echo ($education->type == 'VOCATIONAL')?'selected':'';}?>>Vocational</option>
						<option value="SECONDARY" <? if(isset($education->type)){ echo ($education->type == 'SECONDARY')?'selected':'';}?>>Secondary</option>
						<option value="PRIMARY" <? if(isset($education->type)){ echo ($education->type == 'PRIMARY')?'selected':'';}?>>Primary</option>
						</select>
						<?php echo form_hidden("method", isset($method)?'update':'');?>
					</td>
				</tr>
			</table>
		</td>
		
	</tr>
	<?php } else { ?>
	<tr>
		<td colspan="2">
			<table>
				<tr>
					<td>School:</td>
					<td><input type="text" name="school_name[]" value="">
					<input type="hidden" name="id[]" value="">
					</td>
				</tr>
				<tr>
					<td>Course:</td>
					<td><input type="text" name="course_name[]" value="">
					</td>
				</tr>
				<tr>
					<td>Date Started:</td>
					<td><input type="text" name="date_start[]" id="date_start" value=""></td>
				</tr>
				<tr>
					<td>Date Ended:</td>
					<td><input type="text" name="date_end[]" id="date_end" value=""></td>
				</tr>
				<tr>
					<td>Type:</td>
					<td>
						<select name="type[]">
						<option value="COLLEGE">College</option>
						<option value="VOCATIONAL">Vocational</option>
						<option value="SECONDARY">Secondary</option>
						<option value="PRIMARY">Primary</option>
						</select>
						<?php echo form_hidden("method", 'add');?>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<?php } ?>
	<tr>
		<td colspan="2">
		<?php echo form_hidden("user_id", $user_id);?>
		<input type="submit" name="submit" value="Register"></td>
	</tr>
	
</table>


<?php echo form_close();?>
<?php } ?>
<script>
$(document).ready(function(){
	<?php if(isset($message)):?>
		setTimeout( function() {
			<?php if(isset($method) && $method=='update'){?>
			updateData();
			<?php } else if(isset($method) && $method=='add' ) { ?>
			appendData();
			<?php } else { ?>
			parent.$.fancybox.close();
			<?php }?>
		}, 2000 );
	<?php endif;?>

	<?php if(isset($method) && $method=='update'):?>
	
	function updateData(){
		
		school_name = "<?php echo isset($education)? $education['school_name'][0]: ''?>";
		course_name = "<?php echo isset($education)? $education['course_name'][0]: ''?>";
		date_start = "<?php echo isset($education)? $education['date_start'][0]: ''?>";
		date_end = "<?php echo isset($education)? $education['date_end'][0]: ''?>";
		type = "<?php echo isset($education)? $education['type'][0]: ''?>";
		id = "<?php echo isset($education)? $education['id'][0]: ''?>";
		parent.$('tr#tr_education_' + id).find('span[name^="school_name"]').text(school_name);
		parent.$('tr#tr_education_' + id).find('span[name^="course_name"]').text(course_name);
		parent.$('tr#tr_education_' + id).find('span[name^="date_start"]').text(date_start);
		parent.$('tr#tr_education_' + id).find('span[name^="date_end"]').text(date_end);
		parent.$('tr#tr_education_' + id).find('span[name^="type"]').text(type);
		parent.highlight('tr#tr_education_' + id);
		parent.$.fancybox.close();
		
	}
	<?php endif;?>

<?php if(isset($method) && $method=='add'):?>
	
	function appendData(){
		
		school_name = "<?php echo isset($education)? $education['school_name'][0]: ''?>";
		course_name = "<?php echo isset($education)? $education['course_name'][0]: ''?>";
		date_start = "<?php echo isset($education)? $education['date_start'][0]: ''?>";
		date_end = "<?php echo isset($education)? $education['date_end'][0]: ''?>";
		type = "<?php echo isset($education)? $education['type'][0]: ''?>";
		id = "<?php echo isset($education_id)? $education_id: ''?>";

		tr_count = parent.$('#education_table tr').length;
		css = (tr_count % 2)?'odd':'even';
		
		tr = "<tr id='tr_education_"+ id +"' class='" + css + "'><td>" + school_name + "</td><td>" + course_name;
		tr += "</td><td>" + date_start +"</td><td>" + date_end + "</td><td>" + type + "</td>";
		tr += "<td><a href=''>Edit</a> | <a href=''>Delete</a></td>";
		parent.$('#education_table').append(tr);
		parent.highlight('tr#tr_education_' + id);
		parent.$.fancybox.close();
		
	}
	<?php endif;?>


	// datepicker...
	$("#date_start").datepicker();
	$("#date_end").datepicker();
	$("#date_start").datepicker( "option", "dateFormat", 'yy-mm-dd' );
	$("#date_end").datepicker( "option", "dateFormat", 'yy-mm-dd' );
	<?php if(isset($education->date_start)):?>
	$("#date_start").datepicker( "setDate" , "<?php echo $education->date_start?>" );
	<?php endif; ?>
	<?php if(isset($education->date_end)):?>
	$("#date_end").datepicker( "setDate" , "<?php echo $education->date_end?>" );
	<?php endif; ?>
	
})

</script>
